<?
include 'CoreModel.php';
Class UserModel extends CoreModel {
	public function __construct() {
		parent::__construct();
	}

	public function loginData($data) {
		$this -> db -> select("UserID,Username,Password,SALES_NO,SALES_NAME,ROLE");
		$this -> db -> where($data);
		$query = $this -> db -> get("user");
		if ($query -> num_rows() > 0) {
			return $query -> row(0);
		} else {
			return false;
		}
	}
	public function getUserCount($search) {
		$this -> defaultSearch($search);
		$this -> db -> from("user");
		return $this -> db -> count_all_results();
	}
	public function getUser($search = '', $start = '', $limit = ''){
		$this -> db -> select("*");
		$this -> defaultSearch($search);
		$query = $this -> db -> get("user");
		
		if ($query -> num_rows() > 0) {
			return $query -> result();
		} else {
			return false;
		}
	}
	public function getAUser($userID) {
		$this -> db -> select("*");
		$this->db->where("userID",$userID);
		$query = $this -> db -> get("user");
		if ($query -> num_rows() > 0) {
			return $query -> row(0);
		} else {
			return false;
		}
	}

	public function craeteAuthen($data,$userID) {
		
		$this -> db -> trans_begin();
		// delete old authen;
		$this -> db -> query("delete from tbauthenrole where UserID = '$userID'");
		$this -> db -> insert_batch("tbauthenrole", $data);
		if ($this -> db -> trans_status() === FALSE) {
			$this -> db -> trans_rollback();
			return array("flag" => false, "message" => "error on transfer");
		} else {
			$this -> db -> trans_commit();
			return array("flag" => true, "message" => "create authen Complete");
		}

	}
	public function getRole(){
		$this->db->select("*");
		$queryh = $this->db->get("tbrole");
		return $queryh->result();
		
	}
	public function getAuthen(){
		$this->db->select("*");
		$queryh = $this->db->get("tbauthen");
		return $queryh->result();
		
	}
	public function getMyAuthen($userID){
		
		$this->db->select("*");
		$this->db->where("UserID",$userID);
		$query = $this->db->get("tbauthenrole");
		if($query->num_rows() > 0 ){
			return $query->result();
		}
		return array();
	}
	public function defaultSearch($search) {

		if (empty($search)) {
			return;
		}

		
		if (isset($search['UserID']) && $search['UserID'] != "") {
			$this -> db -> like("UserID", $search['UserID']);
		}
		if (isset($search['SALES_NAME']) && $search['SALES_NAME'] != "") {
			$this -> db -> like("SALES_NAME", $search['SALES_NAME']);
		}
	}
	public function checkUser($data,$notCheckID=''){
		$array = array("UserID","SALES_NO","SALES_NAME","EMAIL","Username");
		$res = array('flag'=>true,'message'=>"");
		foreach($array as $in){
			
			$this->db->select("UserID");
			$this->db->where($in,$data[$in]);
			if($notCheckID!=""){
				$this->db->where("UserID !='$notCheckID'",FALSE,false);
			}
			$query = $this->db->get("user");
			
			if($query->num_rows() > 0 ){
				$res = array('flag'=>false,'message'=>$in ." มีข้อมูลซ้ำแล้วใช้ซ้ำได้");
				break;
			}	
		}
		return $res;
		
		
	}
	public function createUser($data){
		return $this->db->insert("user",$data);
	}
	public function updateUser($data,$idUpdate){
		$this->db->where("UserID",$idUpdate);
		return $this->db->update("user",$data);
	}
}
?>